package com.distributer.policy.controller;

import java.util.LinkedHashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.distributer.policy.model.InsurancePolicy;
import com.distributer.policy.request.PolicyAddRequest;
import com.distributer.policy.request.PolicyRefreshRequest;
import com.distributer.policy.request.PolicyUpdateRequest;
import com.distributer.policy.request.Request;
import com.distributer.policy.service.AsyncHandelerRequestService;
import com.distributer.policy.service.InsurancePolicyService;

@RestController
@RequestMapping("/")
public class InsurancePolicyController {

	private static final Logger LOGGER = LoggerFactory.getLogger(InsurancePolicyController.class);

	@Autowired
	private InsurancePolicyService insurancePolicyService;
	@Autowired
	private AsyncHandelerRequestService asyncHandlerRequestService;
	@Value("${server.port}")
	private String port;
	
	
	@RequestMapping(value = "/policy", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, String> add(@RequestBody InsurancePolicy insurancePolicy) {
		// 封装新增请求
		Request request = new PolicyAddRequest(insurancePolicyService, insurancePolicy);
		asyncHandlerRequestService.process(request);
		Map<String, String> result = new LinkedHashMap<String, String>();
		result.put("jbPolicyNo", insurancePolicy.getJbPolicyNo());
		result.put("code", "0000");
		result.put("msg", "success");
		return result;
	}

	@RequestMapping(value = "/update/policy", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, String> update(InsurancePolicy insurancePolicy) {
		// 封装更新请求
		Request request = new PolicyUpdateRequest(insurancePolicyService, insurancePolicy);
		asyncHandlerRequestService.process(request);
		Map<String, String> result = new LinkedHashMap<String, String>();
		result.put("code", "0000");
		result.put("message", "success");
		return result;
	}

	@RequestMapping(value = "/policy/{jbPolicyNo}", method = RequestMethod.GET)
	@ResponseBody
	public InsurancePolicy findPolicyByJbPolicyNo(@PathVariable(value = "jbPolicyNo", required = true) String jbPolicyNo) {
		// 封装查询请求
		Request request = new PolicyRefreshRequest(insurancePolicyService, jbPolicyNo);
		asyncHandlerRequestService.process(request);
		int waitTime = 0;
		while (true) {
			if (waitTime > 200) {
				break;
			}
			// 查询缓存是否存在保单信息
			InsurancePolicy insurancePolicyFromCache = insurancePolicyService.findInrancePolicyFromCacheByJbPolicyNo(jbPolicyNo);
			if (insurancePolicyFromCache != null) {
				LOGGER.info("钧保保单号： " + jbPolicyNo + " 从缓存获取数据成功！");
				return insurancePolicyFromCache;
			}
			// 休眠20毫秒继续查询
			try {
				Thread.sleep(20);
				waitTime = waitTime + 20;
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		// 直接查询数据库中的数据，并更新缓存
		InsurancePolicy insurancePolicyFromDb = insurancePolicyService.getPolicyByJbPolicyNo(jbPolicyNo);
		if (insurancePolicyFromDb != null) {
			insurancePolicyService.refreshInsrancePolicyCache(insurancePolicyFromDb);
		}
		return insurancePolicyFromDb;
	}
	
	@RequestMapping("/policy/banlance/port")
	@ResponseBody
	public String port(){
		return this.port;
	}
	
}
